/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rhdtp.demopf.control;

import com.rhdtp.demopf.model.dao.UsuarioDAO;
import com.rhdtp.demopf.model.dto.Usuario;
import com.rhdtp.demopf.security.UserSession;
import com.rhdtp.demopf.util.MD5Util;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.apache.log4j.Logger;
import org.primefaces.context.RequestContext;

/**
 *
 * @author rhdtp
 */
public class Perfil {

    private static final Logger logger = Logger.getLogger(Perfil.class);
    private UsuarioDAO usuarioDAO;
    private UserSession userSession;

    public Perfil() {
    }

    public void save(Usuario usuario) {
        usuario.setPass(MD5Util.encriptaMD5(usuario.getPass()));

        usuarioDAO.update(usuario);
        msgInfo("Info.", "Tu Perfil ah sido actualizado correctamente.");

        this.initObjects();
    }

    public void resetForm() {
        initObjects();
        RequestContext.getCurrentInstance().reset("registro:formulario");
    }

    @PostConstruct
    public void initObjects() {
    }

    public void msgInfo(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void msgError(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void msgWarning(String summary, String detail) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, summary, detail);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    /**
     * @param usuarioDAO the usuarioDAO to set
     */
    public void setUsuarioDAO(UsuarioDAO usuarioDAO) {
        this.usuarioDAO = usuarioDAO;
    }

    public UserSession getUserSession() {
        return userSession;
    }

    public void setUserSession(UserSession userSession) {
        this.userSession = userSession;
    }

}
